package com.hbsx.why.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.hbsx.why.R;
import com.hbsx.why.base.BaseActivity;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author LIUB
 * @desc 二维码/条形码
 * @time 2017/8/22 15:23
 */

public class DemoZxingActivity extends BaseActivity {
    @BindView(R.id.tv_topTitle)
    TextView tvTopTitle;
    @BindView(R.id.demo_ewm_text)
    TextView demoEwmText;
    @BindView(R.id.demo_twm_text)
    TextView demoTwmText;
    private Context context = this;
    private int REQUEST_CODE = 200;

    @Override
    protected int getLayoutResId() {
        return R.layout.demo_zxing;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText("二维码/条形码扫描");
    }

    @OnClick({R.id.demo_zxing_ewm, R.id.demo_zxing_txm})
    public void onViewClicked(View view) {
        Intent intent = new Intent(context, CaptureActivity.class);
        switch (view.getId()) {
            case R.id.demo_zxing_ewm:
                startActivityForResult(intent, REQUEST_CODE);
                break;
            case R.id.demo_zxing_txm:
                startActivityForResult(intent, REQUEST_CODE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    demoEwmText.setText("解析结果:" + result);
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    demoEwmText.setText("解析二维码失败!");
                }
            }
        }
    }
}
